public class LinkedMiddle {

    static class Node {
        private Node next;
        private String value;

        public Node(String value) {
            this.value = value;
        }

        public Node() {
        }

        @Override
        public String toString() {
            return "Node{" +
                    "next=" + next +
                    ", value='" + value + '\'' +
                    '}';
        }
    }

    static class LinkList {
        private Node head;

        public LinkList() {
            head = new Node();
        }

        public int length() {
            int length = 0;
            Node p = head.next;
            while (p != null) {
                p = p.next;
                length++;
            }
            return length;
        }

        public Object get(int i) throws Exception {
            int j = 0;
            Node p = head.next;
            while (j < i && p != null) {
                p = p.next;
                j++;
            }
            if (j > i || p == null)
                throw new Exception("查找位置不合理");
            return p.value;
        }

        public void insert(int i, String x) throws Exception {
            Node p = head;
            int j = 0;
            while (j < i && p != null) {
                p = p.next;
                j++;
            }
            if (j > i || p == null)
                throw new Exception("插入位置不合理");
            Node s = new Node(x);
            s.next = p.next;
            p.next = s;
        }

        @Override
        public String toString() {
            return "LinkList{" +
                    "head=" + head +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        LinkList linkList = new LinkList();
        for (int i = 0; i < 11; i++) {
            linkList.insert(i, String.valueOf(i));
        }
        System.out.println(linkList);
        int fastPoint = 0;
        int slowPoint = 0;
        while (fastPoint < linkList.length()) {
            fastPoint += 2;
            slowPoint++;
        }
        System.out.println(linkList.get(slowPoint));
    }
}
